package com.etc.service;

import com.etc.dao.CommentsDao;
import com.etc.dao.MoviesDao;
import com.etc.entity.Comments;
import com.etc.entity.Movies;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Transactional
@Service
public class CommentsServiceImpl implements CommentsService {
    @Autowired
    CommentsDao commentsDao;
    @Autowired
    MoviesDao moviesDao;

    @Transactional
    @Override
    public boolean addComment(Comments comments) {
        int n = commentsDao.addComment(comments);
        return n > 0;
    }

    @Override
    public PageInfo<Comments> queryAllByMovie(Integer movie_no, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Comments> list = commentsDao.queryAllByMovie(movie_no);
        PageInfo<Comments> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Transactional
    @Override
    public boolean delComment(String comment_no) {
        int n = commentsDao.delComment(comment_no);
        return n > 0;
    }

    @Override
    public String avgScore(Integer movie_no) {
        return commentsDao.avgScore(movie_no);
    }

    @Override
    public Comments queryDtail(Integer user_no, String order_no) {
        Comments comments = commentsDao.queryDtail(user_no, order_no);
        if (comments == null) {
            comments = new Comments("0", null, null, 0, null, 0, 0.0, null, "0", null);
        }
        return comments;
    }

    @Override
    public boolean updateComment(Comments comments) {
        int n = commentsDao.updateComment(comments);
        return n > 0;
    }

    public List<Movies> avgAll() {
        List<Movies> list = moviesDao.queryAllMoviesByName("");
        for (Movies m : list) {
            String s = commentsDao.avgScore(m.getMovieNo());
            m.setMovieScore(s);
        }
        return list;
    }
}
